#include "arguments.h"

#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <config.h>

#include "global.h"

// Display help message and exit.
static void display_help()
{
	printf(_("Usage: %s [options]\n"), PACKAGE);
	printf(_("Options:\n"));
	printf("  -h,  --help        %s", _("display this help and exit\n"));
	printf("  -v,  --version     %s", _("output version information and exit\n"));
	exit(0);
}


// Display version and exit.
static void display_version()
{
	printf(PACKAGE " " VERSION "\n");
	printf(_("This is free software.\n"));
	printf("Copyright (C) 2010 André Wagner\n");
	exit(0);
}


// Parse the list of arguments, store the global parameters and/or take the
// apropriate actions.
void arguments(int argc, char** argv)
{

	int c;

	while(1)
	{
		static struct option long_options[] =
		{
			{ "help",    no_argument, 0, 'h' },
			{ "version", no_argument, 0, 'v' },
			{ "debug",   no_argument, 0, 'd' },
			{ 0, 0, 0, 0 },
		};

		int idx = 0;

		c = getopt_long(argc, argv, "hvd", long_options, &idx);
		if(c == -1)
			break;

		switch(c)
		{
			case 'h':
				display_help();
				break;
			case 'v':
				display_version();
				break;
			case 'd':
				debug = true;
				break;
			case '?':
				break;
			default:
				abort();
		}

		if(optind < argc)
		{
			fprintf(stderr, "Invalid option: %s\n", argv[optind+1]);
			exit(1);
		}
	}
}
